package com.study.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.study.dao.MemberDao;
import com.study.pojo.Member;
import com.study.service.MemberService;
import com.study.utils.MD5Utils;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Resource(name = "memberDao")
    private MemberDao memberDao;

    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        String password = member.getPassword();
        if(password != null){
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberDao.add(member);
    }

    @Override
    public Map<String, Object> getMemberReport() {
        Map<String, Object> map = new HashMap<>();
        List<String> monthsList = new ArrayList<>();
        List<Integer> memberCountsList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,-12);
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH,1);
            Date date = calendar.getTime();
            monthsList.add(new SimpleDateFormat("yyyy.MM").format(date));
            memberCountsList.add(memberDao.findMemberCountByMonth(new SimpleDateFormat("yyyy.MM").format(date).concat(".31")));
        }

        map.put("months",monthsList);
        map.put("memberCounts",memberCountsList);
        return map;
    }
}
